package com.cskaoyan.service.impl;

import com.cskaoyan.bean.issue.bo.IssueAskBo;
import com.cskaoyan.bean.issue.po.IssuePo;
import com.cskaoyan.bean.issue.vo.IssueInfoVo;
import com.cskaoyan.bean.param.BaseParam;
import com.cskaoyan.mapper.IssueMapper;
import com.cskaoyan.service.IssueService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Date;
import java.util.List;

/**
 * @author LiangRuiJiang
 * @since 2022/06/04  18:33
 */
@Service
public class IssueServiceImpl implements IssueService {

    @Autowired
    IssueMapper issueMapper;

    @Override
    public IssueInfoVo query(BaseParam param, String question) {
        //  在执行查询之前，可以开启分页
        PageHelper.startPage(param.getPage(),param.getLimit());
        //  使用
        List<IssuePo> issuePos = issueMapper.selectIssueByQuestion(param.getSort(), param.getOrder(), question);

        PageInfo pageInfo = new PageInfo(issuePos);

        //不分页情况下的数量
        long total = pageInfo.getTotal();
        //总的页码数
        int pages = pageInfo.getPages();

        return new IssueInfoVo(total,pages,param.getLimit(),param.getPage(),issuePos);
    }

    @Override
    @Transactional
    public IssuePo addIssue(IssueAskBo issue) {
        IssuePo issuePo = new IssuePo(false, issue.getQuestion(), issue.getAnswer(), new Date(), null, null);
        issueMapper.insertIssue(issuePo);
        return issuePo;
    }

    @Override
    @Transactional
    public IssuePo updateIssue(IssuePo issuePo) {
        issuePo.setUpdateTime(new Date());
        issueMapper.updateIssueDetail(issuePo);
        return issuePo;
    }

    @Override
    public void deleteIssue(IssuePo issuePo) {
        issueMapper.updateIssueDeletedById(issuePo.getId());
    }
}
